/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import shared.MakaoPackage;

/**
 *
 * @author latata_2
 */
public class PlayerConnection extends Thread {
    
    private Server server;
    private Socket connection;
    private InputStream input = null;
    private OutputStream output = null;
    ObjectOutputStream objectOutputStream = null;
    
    private InetAddress addressIP;
    private int playerId;

    public PlayerConnection(Server server, Socket connection, int id) {
        this.server = server;
        try {
            this.connection = connection;
            this.addressIP = connection.getInetAddress();
            this.playerId = id;
            input = connection.getInputStream();
            output = connection.getOutputStream();
            objectOutputStream = new ObjectOutputStream(output);
        } catch (IOException ex) {
            Logger.getLogger(PlayerConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(input);  
            while (true) {
                MakaoPackage makaoPackage = (MakaoPackage) objectInputStream.readObject();
                if (makaoPackage != null) {

                    if(makaoPackage.getCommand() >= 0 && makaoPackage.getValues() != null) {
                        runCommand(makaoPackage.getCommand(), makaoPackage.getValues());
                    } else if(makaoPackage.getThorwCards() != null) {
                        server.throwCards(this, makaoPackage.getThorwCards());
                    } else if(makaoPackage.getGetCards()) {
                        server.getCards(this);
                    } else if(makaoPackage.getWaitTurns()) {
                        server.waitTurns(this);
                    } 
                }
            }
        } catch (ClassNotFoundException ex) {
            //System.out.println("Wystąpił błąd!");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("Klient rozlaczony!");
        } finally {
            try {
                objectInputStream.close();
                input.close();
                output.close();
                disconnect();
            } catch (IOException ex) {
                Logger.getLogger(PlayerConnection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void disconnect() {
        try {
            connection.close();
            server.removePlayer(this);
        } catch (IOException ex) {
            Logger.getLogger(PlayerConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void send(MakaoPackage o) {
        try {
            
            objectOutputStream.writeObject(o);
            objectOutputStream.flush();
        } catch (IOException ex) {
            Logger.getLogger(PlayerConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public InetAddress getAddressIP() {
        return addressIP;
    }

    public int getPlayerId() {
        return playerId;
    }

    private void runCommand(int command, ArrayList<String> values) {
        switch(command) {
            case MakaoPackage.LOGIN:
                server.newPlayerLogin(this, values.get(0));
                break;
        }
    }
    
    
    
    
}